package app.imttf.online.backing.buatttf;

import app.imttf.online.adfextensions.ADFUtils;

import app.imttf.online.backing.utils.BackingUtil;

import oracle.adf.model.BindingContext;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;
import oracle.adf.view.rich.event.PopupFetchEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

public class TtfNonPkpCrudBackingBean {
    private RichTable nonPkpTable;

    public TtfNonPkpCrudBackingBean() {
    }

    public void editPopupFetchListener(PopupFetchEvent popupFetchEvent) {
        if (popupFetchEvent.getLaunchSourceClientId().contains("cbInsert")) {
            BindingContainer bindings = getBindings();
            OperationBinding operationBinding = bindings.getOperationBinding("CreateInsert");
            operationBinding.execute();
        }
    }

    public void editPopupCancelListener(PopupCanceledEvent popupCanceledEvent) {
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding = bindings.getOperationBinding("Rollback");
        operationBinding.execute();
    }

    public void editDialogListener(DialogEvent dialogEvent) {
        if (dialogEvent.getOutcome().name().equals("ok")) {
            BindingContainer bindings = getBindings();
            OperationBinding operationBinding = bindings.getOperationBinding("Commit");
            operationBinding.execute();   
            AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
            adfFacesContext.addPartialTarget(nonPkpTable);
        } else if (dialogEvent.getOutcome().name().equals("cancel")) {
            BindingContainer bindings = getBindings();
            OperationBinding operationBinding = bindings.getOperationBinding("Rollback");
            operationBinding.execute();
            AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
            adfFacesContext.addPartialTarget(nonPkpTable);
        }
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void setNonPkpTable(RichTable nonPkpTable) {
        this.nonPkpTable = nonPkpTable;
    }

    public RichTable getNonPkpTable() {
        return nonPkpTable;
    }

    public void confirmDeleteBpbNonPkp(DialogEvent dialogEvent) {
        BackingUtil.confirmDeleteDialogListener(dialogEvent, getNonPkpTable(), "VerifikasiNewTtfNonPkpView1");
        ADFUtils.invokeEL("#{bindings.Commit.execute}");
    }
}
